//
//  Date+ZMExtension.swift
//  TravelForDriver
//
//  Created by Rowling on 2020/9/23.
//  Copyright © 2020 智蚂优服. All rights reserved.
//

import UIKit

extension Date : ZMCompatible {}

extension ZM where Base == Date {
    func toString(_ format : String) -> String {
        let fo = DateFormatter()
        fo.dateFormat = format
        fo.locale = Locale.init(identifier: "en_US_POSIX")
        fo.calendar = Calendar.init(identifier: Calendar.Identifier.gregorian)
        return fo.string(from: base)
    }
    
    
    /// 获取日期是星期几
    /// - Returns: 1 是周日，2是周一 3.以此类推
    func weekday() -> Int {
        let calenndar = Calendar.current
        let weekDay = calenndar.component(Calendar.Component.weekday, from: base)
        return weekDay
    }
    
    func weekdayStr() -> String {
        let weekDay = weekday()
        switch weekDay {
            case 1:
                return "日"
            case 2:
                return "一"
            case 3:
                return "二"
            case 4:
                return "三"
            case 5:
                return "四"
            case 6:
                return "五"
            case 7:
                return "六"
            default:return""
        }
    }
    
    func normalDesTimeStr() -> String {
        let dateFormatter = DateFormatter()
        dateFormatter.dateFormat = "yyyy-MM-dd"
        let toDayStr = dateFormatter.string(from: Date())
        let theDayStr = dateFormatter.string(from: base)
        let timeInterval = -base.timeIntervalSinceNow
        
        if toDayStr == theDayStr {
            // 同一天
            if timeInterval < 0 {
                return ""
            }
            if timeInterval < 60 {
                return "刚刚";
            } else if timeInterval < 3600 {
                return "\(Int(timeInterval / 60))分钟前"
            }
            //else if timeInterval < 21600 {
//                return "\(Int(timeInterval / 3600))小时前"
//            }
            else {
                dateFormatter.dateFormat = "HH:mm"
                return "\(dateFormatter.string(from: base))"
            }
        } else if let theDay = dateFormatter.date(from: theDayStr),
                  let toDay = dateFormatter.date(from: toDayStr),
                  toDay.timeIntervalSince(theDay) == 86400 {
            //one day ago
            if timeInterval < 60 * 60 * 24 {
                dateFormatter.dateFormat = "HH:mm"
                return "昨天 \(dateFormatter.string(from: base))"
            } else {
                dateFormatter.dateFormat = "yyyy-MM-dd HH:mm"
                return dateFormatter.string(from: base)
            }
        } else {
            dateFormatter.dateFormat = "yyyy-MM-dd HH:mm"
            return dateFormatter.string(from: base)
        }
    }
    
    /// 获取当天凌晨的时间
    func getTheDayStart() -> Date? {
       var cal = Calendar.current
       cal.locale = Locale.init(identifier: "en_US_POSIX")
       var zercomponents = cal.dateComponents([.year, .month, .day, .hour, .minute, .second], from: base)
       zercomponents.hour = 0
       zercomponents.minute = 0
       zercomponents.second = 0
       return cal.date(from: zercomponents)
    }
}
